
package com.hexinfo.dmpro.sparing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.utils.CommonConstants;
import com.hexinfo.dmpro.sparing.model.SyncAuditWhiteList;
import com.hexinfo.dmpro.sparing.dao.SyncAuditWhiteListMapper;
import com.hexinfo.dmpro.sparing.service.SyncAuditWhiteListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 全量稽核白名单表
 *
 * @author yemw
 * @date 2024-06-12 13:46:20
 */
@Service
@Slf4j
public class SyncAuditWhiteListServiceImpl extends ServiceImpl<SyncAuditWhiteListMapper, SyncAuditWhiteList> implements SyncAuditWhiteListService {

    @Override
    public Page queryList(Page page, SyncAuditWhiteList syncAuditWhiteList) {
        LambdaQueryWrapper<SyncAuditWhiteList> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(syncAuditWhiteList.getTblDatabaseName()),SyncAuditWhiteList::getTblDatabaseName,syncAuditWhiteList.getTblDatabaseName())
                .like(StrUtil.isNotBlank(syncAuditWhiteList.getTblName()),SyncAuditWhiteList::getTblName,syncAuditWhiteList.getTblName())
                .eq(SyncAuditWhiteList::getDel,CommonConstants.STATUS_NORMAL).orderByDesc(SyncAuditWhiteList::getCreateTime);
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        return returnPage;
    }

    @Override
    public boolean remove(String id) {
        SyncAuditWhiteList syncAuditWhiteList = new SyncAuditWhiteList();
        syncAuditWhiteList.setId(id);
        syncAuditWhiteList.setDel(CommonConstants.STATUS_DEL);
        int rem = this.baseMapper.updateById(syncAuditWhiteList);
        if(rem>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<String> addWhiteList(String dbName) {
        List<String> list = new ArrayList<>();
        LambdaQueryWrapper<SyncAuditWhiteList> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(dbName),SyncAuditWhiteList::getTblDatabaseName,dbName)
                .eq(SyncAuditWhiteList::getDel,CommonConstants.STATUS_NORMAL);
        List<SyncAuditWhiteList> selectList = this.baseMapper.selectList(wrapper);
        if(ObjectUtil.isNotEmpty(selectList)){
            for (SyncAuditWhiteList syncAuditWhiteList : selectList) {
                list.add(syncAuditWhiteList.getTblDatabaseName()+'.'+syncAuditWhiteList.getTblName());
                log.warn("白名单库表："+syncAuditWhiteList.getTblDatabaseName()+'.'+syncAuditWhiteList.getTblName());
            }
        }
        return list;
    }
}
